<template>
  <div class="exam">
    <el-table :data="pagination.records" border>
      <el-table-column fixed="left" prop="source" label="试卷名称" width="180"></el-table-column>
      <el-table-column prop="description" label="介绍" width="200"></el-table-column>
      <el-table-column prop="institute" label="所属学院" width="120"></el-table-column>
      <el-table-column prop="major" label="所属专业" width="200"></el-table-column>
      <el-table-column prop="grade" label="年级" width="100"></el-table-column>
      <el-table-column prop="examDate" label="考试日期" width="120"></el-table-column>
      <el-table-column prop="totalTime" label="持续时间" width="120"></el-table-column>
      <el-table-column prop="totalScore" label="总分" width="120"></el-table-column>
      <el-table-column prop="type" label="试卷类型" width="120"></el-table-column>
      <el-table-column prop="tips" label="考生提示" width="400"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template v-slot:default="scope">
          <el-button @click="edit(scope.row.examCode)" type="primary" size="small">编辑</el-button>
          <el-button @click="deleteRecord(scope.row.examCode)" type="danger" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[4, 8, 10, 20]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total" class="page">
    </el-pagination>
    <!-- 编辑对话框-->
    <el-dialog
      title="编辑试卷信息"
      v-model:visible="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="试卷名称">
            <el-input v-model="form.source"></el-input>
          </el-form-item>
          <el-form-item label="介绍">
            <el-input v-model="form.description"></el-input>
          </el-form-item>
          <el-form-item label="所属学院">
            <el-input v-model="form.institute"></el-input>
          </el-form-item>
          <el-form-item label="所属专业">
            <el-input v-model="form.major"></el-input>
          </el-form-item>
          <el-form-item label="年级">
            <el-input v-model="form.grade"></el-input>
          </el-form-item>
          <el-form-item label="考试日期">
            <el-col :span="11">
              <el-date-picker type="date" placeholder="选择日期" v-model="form.examDate" style="width: 100%;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="持续时间">
            <el-input v-model="form.totalTime"></el-input>
          </el-form-item>
          <el-form-item label="总分">
            <el-input v-model="form.totalScore"></el-input>
          </el-form-item>
          <el-form-item label="试卷类型">
            <el-input v-model="form.type"></el-input>
          </el-form-item>
          <el-form-item label="考生提示">
            <el-input type="textarea" v-model="form.tips"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <template v-slot:footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted, getCurrentInstance } from 'vue';

export default {
  name: 'ExamList', // 组件名称
  setup() {
    const form = ref({}); // 保存点击以后当前试卷的信息
    const dialogVisible = ref(false); // 控制对话框显示
    const pagination = ref({
      current: 1, // 当前页
      total: null, // 记录条数
      size: 4, // 每页条数
      records: [] // 存储试卷数据
    });

    // 获取当前Vue实例的上下文，访问 $axios
    const { proxy } = getCurrentInstance();

    // 获取所有考试信息
    const getExamInfo = () => {
      proxy.$axios(`/api/exams/${pagination.value.current}/${pagination.value.size}`)
        .then((res) => {
          pagination.value = res.data.data;
        })
        .catch((error) => {
          console.log(error);
        });
    };

    // 编辑试卷
    const edit = (examCode) => {
      dialogVisible.value = true;
      proxy.$axios(`/api/exam/${examCode}`).then((res) => {
        if (res.data.code === 200) {
          form.value = res.data.data;
        }
      });
    };

    // 提交修改后的试卷信息
    const submit = () => {
      dialogVisible.value = false;
      proxy.$axios({
        url: '/api/exam',
        method: 'put',
        data: {
          ...form.value,
        },
      }).then((res) => {
        if (res.data.code === 200) {
          proxy.$message({ // 成功修改提示
            message: '更新成功',
            type: 'success',
          });
        }
        getExamInfo();
      });
    };

    // 删除记录
    const deleteRecord = (examCode) => {
      proxy.$confirm("确定删除该记录吗,该操作不可逆！！！", "删除提示", {
        confirmButtonText: '确定删除',
        cancelButtonText: '算了,留着',
        type: 'danger',
      }).then(() => {
        proxy.$axios({
          url: `/api/exam/${examCode}`,
          method: 'delete',
        }).then((res) => {
          console.log(res);
          getExamInfo();
        });
      }).catch(() => {
        // Handle cancel
      });
    };

    // 改变当前记录条数
    const handleSizeChange = (val) => {
      pagination.value.size = val;
      getExamInfo();
    };

    // 改变当前页码，重新发送请求
    const handleCurrentChange = (val) => {
      pagination.value.current = val;
      getExamInfo();
    };

    // 关闭提醒
    const handleClose = (done) => {
      proxy.$confirm('确认关闭？').then(() => {
        done();
      }).catch(() => {});
    };

    // 页面加载时获取数据
    onMounted(() => {
      getExamInfo();
    });

    return {
      form,
      pagination,
      dialogVisible,
      edit,
      submit,
      deleteRecord,
      handleSizeChange,
      handleCurrentChange,
      handleClose,
    };
  },
};
</script>


<style lang="less" scoped>
.exam {
  padding: 0px 40px;
  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .edit {
    margin-left: 20px;
  }
}
</style>
